﻿using NxERP.Bll;

using NxStoreManage;

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;

namespace LengthAiCalculate
{
    public partial class CalculateMain : BaseForm
    {
        private CalculationConfig calculationConfig1 = null;
        private CalculationConfig calculationConfig2 = null;
        private CalculationConfig calculationConfig3 = null;
        private CalculationConfig calculationConfig4 = null;
        private CalculationConfig calculationConfig5 = null;
        private CalculationConfig calculationConfig6 = null;
        private CalculationConfig calculationConfig7 = null;
        private CalculationConfig calculationConfig8 = null;
        private CalculationConfig calculationConfig9 = null;
        private CalculationConfig calculationConfig10 = null;
        private CalculationConfig calculationConfig11 = null;

        private static string Jhorderby = "num desc";
        private static string ScOrderBy = "num desc";
        private DataTable scdt = null;
        private DataTable jhdt = null;
        private ResultForm resultForm1 = null;
        private ResultForm resultForm2 = null;
        private ResultForm resultForm3 = null;
        private ResultForm resultForm4 = null;
        private ResultForm resultForm5 = null;
        private ResultForm resultForm6 = null;
        private ResultForm resultForm7 = null;
        private ResultForm resultForm8 = null;
        private ResultForm resultForm9 = null;
        private ResultForm resultForm10 = null;
        private ResultForm resultForm11 = null;

        private AiResult result1 = null;
        private AiResult result2 = null;
        private AiResult result3 = null;
        private AiResult result4 = null;
        private AiResult result5 = null;
        private AiResult result6 = null;
        private AiResult result7 = null;
        private AiResult result8 = null;
        private AiResult result9 = null;
        private AiResult result10 = null;
        private AiResult result11 = null;

        private Dictionary<double, ResultTable> resultTable = new Dictionary<double, ResultTable>();

        public CalculateMain()
        {
            InitializeComponent();
            toolStripStatusLabel2.Text = "软件设计：刘锋";

#if DEBUG
            toolStripStatusLabel1.Text = "当前版本:Debug" + "；当前版本支持单根最大3种尺寸(分段数量不限)，算法难度A=n^n，n最大尺寸数量";
#else
            toolStripStatusLabel1.Text = "当前版本:v" + System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString() + "；当前版本支持单根最大3种尺寸(分段数量不限)，算法难度A=n^n，n最大尺寸数量";

#endif
            double.TryParse(textBox7.Text, out double minlength);
            if (minlength <= 0)
            {
                minlength = 6;
            }
            double.TryParse(textBox8.Text, out double maxlength);
            if (maxlength <= 0)
            {
                maxlength = 18;
            }
            double.TryParse(textBox10.Text, out double lengthStep);
            if (lengthStep <= 0)
            {
                lengthStep = 1;
            }
            calculationConfig1 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig2 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig3 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig4 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig5 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig6 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig7 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig8 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig9 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig10 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
            calculationConfig11 = new CalculationConfig
            {
                CutNum = 8,
                JhOrderBy = Jhorderby,
                ScOrderBy = ScOrderBy,
                RadixPoint = 2,
                NoPlanDifficultyValue = 1,
                PlanDifficultyValue = 2,
                JhDataTable = null,
                ScDataTable = null,
                MinLength = minlength,
                MaxLength = maxlength,
                LengthStep = lengthStep
            };
        }

        private void SetValue(CalculationConfig calculationConfig)
        {
            if (int.TryParse(textBox1.Text, out int CutNum) == true && CutNum > 2)
            {
                calculationConfig.CutNum = CutNum;
            }
            if (int.TryParse(textBox2.Text, out int NoPlanDifficultyValue) == true)
            {
                calculationConfig.NoPlanDifficultyValue = NoPlanDifficultyValue;
            }
            if (int.TryParse(textBox3.Text, out int PlanDifficultyValue) == true)
            {
                calculationConfig.PlanDifficultyValue = PlanDifficultyValue;
            }
            if (int.TryParse(textBox4.Text, out int RadixPoint) == true && RadixPoint > 0)
            {
                calculationConfig.RadixPoint = RadixPoint;
            }
            int.TryParse(textBox9.Text, out int groupNum);
            if (groupNum < 1)
            {
                groupNum = 1;
            }
            calculationConfig.GroupNum = groupNum;
            double.TryParse(textBox7.Text, out double minlength);
            if (minlength <= 0)
            {
                minlength = 6;
            }
            double.TryParse(textBox8.Text, out double maxlength);
            if (maxlength <= 0)
            {
                maxlength = 18;
            }
            double.TryParse(textBox10.Text, out double lengthStep);
            if (lengthStep <= 0)
            {
                lengthStep = 1;
            }
            calculationConfig.MinLength = minlength;
            calculationConfig.MaxLength = maxlength;
            calculationConfig.LengthStep = lengthStep;
        }

        private void SetOpenLabel(bool flag)
        {
            if (flag)
            {
                label29.Cursor = Cursors.Default;
                label30.Cursor = Cursors.Default;
                label31.Cursor = Cursors.Default;
                label32.Cursor = Cursors.Default;
                label33.Cursor = Cursors.Default;
                label34.Cursor = Cursors.Default;
                label35.Cursor = Cursors.Default;
                label36.Cursor = Cursors.Default;
                label37.Cursor = Cursors.Default;
                label45.Cursor = Cursors.Default;
                label58.Cursor = Cursors.Default;

                g1.Enabled = false;
                g2.Enabled = false;
                g3.Enabled = false;
                g4.Enabled = false;
                g5.Enabled = false;
                g6.Enabled = false;
                g7.Enabled = false;
                g8.Enabled = false;
                g9.Enabled = false;
                g10.Enabled = false;
                g11.Enabled = false;
            }
            else
            {
                label29.Cursor = Cursors.Hand;
                label30.Cursor = Cursors.Hand;
                label31.Cursor = Cursors.Hand;
                label32.Cursor = Cursors.Hand;
                label33.Cursor = Cursors.Hand;
                label34.Cursor = Cursors.Hand;
                label35.Cursor = Cursors.Hand;
                label36.Cursor = Cursors.Hand;
                label37.Cursor = Cursors.Hand;
                label45.Cursor = Cursors.Hand;
                label58.Cursor = Cursors.Hand;

                g1.Enabled = true;
                g2.Enabled = true;
                g3.Enabled = true;
                g4.Enabled = true;
                g5.Enabled = true;
                g6.Enabled = true;
                g7.Enabled = true;
                g8.Enabled = true;
                g9.Enabled = true;
                g10.Enabled = true;
                g11.Enabled = true;
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (Control ctl in groupBox4.Controls)
                {
                    if (ctl is GroupBox)
                    {
                        ctl.ForeColor = System.Drawing.Color.Black;
                        foreach (Control lab in ctl.Controls)
                        {
                            if (lab is Label)
                            {
                                string txt = lab.Text.Trim();
                                if (txt.Substring(0, 2) == "计划")
                                {
                                    lab.Text = "计划完成率：0%";
                                }
                                else if (txt.Substring(0, 2) == "非计")
                                {
                                    lab.Text = "非计划率：0%";
                                }
                                else if (txt.Substring(0, 2) == "生产")
                                {
                                    lab.Text = "生产困难度：0";
                                }
                            }
                        }
                    }
                }
                string path = openFileDialog1.FileName;
                if (NopiHelp.CheckSheetName(path, "原料") && NopiHelp.CheckSheetName(path, "订单"))
                {
                    DataTable scdt = NopiHelp.ExcelToDataTable(path, "原料", true);
                    scdt.Rows.RemoveAt(0);
                    DataTable jhdt = NopiHelp.ExcelToDataTable(path, "订单", true);
                    jhdt.Rows.RemoveAt(0);
                    if (scdt.Columns.Contains("lg") == false)
                    {
                        MessageBox.Show("SB_Ai_SC(大料)表缺少lg(长度)列！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (scdt.Columns.Contains("num") == false)
                    {
                        MessageBox.Show("SB_Ai_SC(大料)表缺少num(支数)列！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (jhdt.Columns.Contains("lg") == false)
                    {
                        MessageBox.Show("SB_Ai_JH(订单计划)表缺少lg(长度)列！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    if (jhdt.Columns.Contains("num") == false)
                    {
                        MessageBox.Show("SB_Ai_JH(订单计划)表缺少num(支数)列！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    if (double.TryParse(jhdt.Rows[0]["lg"].ToString(), out _) == false)
                    {
                        textBox5.Text = jhdt.Rows[0]["lg"].ToString();
                        textBox6.Text = jhdt.Rows[0]["num"].ToString();
                        jhdt.Rows.RemoveAt(0);
                    }
                    else
                    {
                        textBox5.Text = "";
                        textBox6.Text = "";
                    }
                    if (double.TryParse(scdt.Rows[0]["lg"].ToString(), out _) == false)
                    {
                        textBox16.Text = scdt.Rows[0]["lg"].ToString();
                        textBox15.Text = scdt.Rows[0]["num"].ToString();
                        scdt.Rows.RemoveAt(0);
                    }
                    else
                    {
                        textBox16.Text = "";
                        textBox15.Text = "";
                    }
                    dataGridView1.DataSource = scdt;
                    dataGridView1.Columns[1].HeaderText = "支数";
                    dataGridView1.Columns[0].HeaderText = "长度";
                    dataGridView2.DataSource = jhdt;
                    dataGridView2.Columns[1].HeaderText = "支数";
                    dataGridView2.Columns[0].HeaderText = "长度";
                    foreach (DataRow row in jhdt.Rows)
                    {
                        double length = double.Parse(row["lg"].ToString());
                        int num = int.Parse(row["num"].ToString());
                        if (resultTable.ContainsKey(length))
                        {
                            resultTable[length].JhNum += num;
                        }
                        else
                        {
                            resultTable.Add(length, new ResultTable { JhNum = num, RealNum = 0 });
                        }
                    }
                }
                else
                {
                    MessageBox.Show("打开文件失败或者缺少原料表或者订单表！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void ClosedForm(ResultForm resultForm)
        {
            if (resultForm != null)
            {
                resultForm.Close();
                resultForm.Dispose();
            }
        }

        private string SheetName()
        {
            string sheetName = null;
            if (textBox5.Text.Trim() != "" || textBox6.Text.Trim() != "")
            {
                sheetName = textBox5.Text.Trim() + "_" + textBox6.Text.Trim();
            }
            return sheetName;
        }

        private void C()
        {
            BoolConfig boolConfig = new BoolConfig
            {
                IsFreedomCount = checkBox1.Checked,
                IsFreedomLength = checkBox2.Checked,
                IsPlanConfirm = checkBox3.Checked
            };
            if (checkBox4.Checked)
            {
                Jhorderby = "lg desc";
            }
            else
            {
                Jhorderby = "num desc";
            }
            if (checkBox5.Checked)
            {
                ScOrderBy = "lg desc";
            }
            else
            {
                ScOrderBy = "num desc";
            }
            calculationConfig1.JhOrderBy = Jhorderby;
            calculationConfig1.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig1.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig1);

            calculationConfig2.JhOrderBy = Jhorderby;
            calculationConfig2.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig2.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig2);

            calculationConfig3.JhOrderBy = Jhorderby;
            calculationConfig3.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig3.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig3);

            calculationConfig4.JhOrderBy = Jhorderby;
            calculationConfig4.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig4.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig4);

            calculationConfig5.JhOrderBy = Jhorderby;
            calculationConfig5.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig5.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig5);

            calculationConfig6.JhOrderBy = Jhorderby;
            calculationConfig6.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig6.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig6);

            calculationConfig7.JhOrderBy = Jhorderby;
            calculationConfig7.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig7.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig7);

            calculationConfig8.JhOrderBy = Jhorderby;
            calculationConfig8.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig8.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig8);

            calculationConfig9.JhOrderBy = Jhorderby;
            calculationConfig9.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig9.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig9);

            calculationConfig10.JhOrderBy = Jhorderby;
            calculationConfig10.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig10.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig10);

            calculationConfig11.JhOrderBy = Jhorderby;
            calculationConfig11.JhDataTable = jhdt.Select().CopyToDataTable();
            calculationConfig11.ScDataTable = scdt.Select().CopyToDataTable();
            SetValue(calculationConfig11);

            NxERP.Bll.LengthAiCalculate lengthAiCalculate1 = new NxERP.Bll.LengthAiCalculate(calculationConfig1);
            result1 = lengthAiCalculate1.CalculationModule_1_2_3(boolConfig);
            label5.Text = "计划完成率:" + result1.AchievePercent.ToString() + "%";
            label6.Text = "非计划率:" + result1.NoAchievePercent.ToString() + "%";
            label7.Text = "生产难度:" + result1.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate2 = new NxERP.Bll.LengthAiCalculate(calculationConfig2);
            result2 = lengthAiCalculate2.CalculationModule_1_3_2(boolConfig);
            label8.Text = "计划完成率:" + result2.AchievePercent.ToString() + "%";
            label10.Text = "非计划率:" + result2.NoAchievePercent.ToString() + "%";
            label9.Text = "生产难度:" + result2.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate3 = new NxERP.Bll.LengthAiCalculate(calculationConfig3);
            result3 = lengthAiCalculate3.CalculationModule_2_1_3(boolConfig);
            label11.Text = "计划完成率:" + result3.AchievePercent.ToString() + "%";
            label13.Text = "非计划率:" + result3.NoAchievePercent.ToString() + "%";
            label12.Text = "生产难度:" + result3.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate4 = new NxERP.Bll.LengthAiCalculate(calculationConfig4);
            result4 = lengthAiCalculate4.CalculationModule_2_3_1(boolConfig);
            label14.Text = "计划完成率:" + result4.AchievePercent.ToString() + "%";
            label16.Text = "非计划率:" + result4.NoAchievePercent.ToString() + "%";
            label15.Text = "生产难度:" + result4.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate5 = new NxERP.Bll.LengthAiCalculate(calculationConfig5);
            result5 = lengthAiCalculate5.CalculationModule_3_2_1(boolConfig);
            label17.Text = "计划完成率:" + result5.AchievePercent.ToString() + "%";
            label19.Text = "非计划率:" + result5.NoAchievePercent.ToString() + "%";
            label18.Text = "生产难度:" + result5.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate6 = new NxERP.Bll.LengthAiCalculate(calculationConfig6);
            result6 = lengthAiCalculate6.CalculationModule_3_1_2(boolConfig);
            label20.Text = "计划完成率:" + result6.AchievePercent.ToString() + "%";
            label22.Text = "非计划率:" + result6.NoAchievePercent.ToString() + "%";
            label21.Text = "生产难度:" + result6.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate7 = new NxERP.Bll.LengthAiCalculate(calculationConfig7);
            result7 = lengthAiCalculate7.OneLength();
            label23.Text = "计划完成率:" + result7.AchievePercent.ToString() + "%";
            label25.Text = "非计划率:" + result7.NoAchievePercent.ToString() + "%";
            label24.Text = "生产难度:" + result7.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate8 = new NxERP.Bll.LengthAiCalculate(calculationConfig8);
            result8 = lengthAiCalculate8.TwoLength();
            label26.Text = "计划完成率:" + result8.AchievePercent.ToString() + "%";
            label28.Text = "非计划率:" + result8.NoAchievePercent.ToString() + "%";
            label27.Text = "生产难度:" + result8.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate9 = new NxERP.Bll.LengthAiCalculate(calculationConfig9);
            result9 = lengthAiCalculate9.ThreeLength();
            label38.Text = "计划完成率:" + result9.AchievePercent.ToString() + "%";
            label40.Text = "非计划率:" + result9.NoAchievePercent.ToString() + "%";
            label39.Text = "生产难度:" + result9.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate10 = new NxERP.Bll.LengthAiCalculate(calculationConfig10);
            result10 = lengthAiCalculate10.CalculationModule_1_2_F(boolConfig);
            label46.Text = "计划完成率:" + result10.AchievePercent.ToString() + "%";
            label48.Text = "非计划率:" + result10.NoAchievePercent.ToString() + "%";
            label47.Text = "生产难度:" + result10.DifficultyValue.ToString();

            NxERP.Bll.LengthAiCalculate lengthAiCalculate11 = new NxERP.Bll.LengthAiCalculate(calculationConfig11);
            result11 = lengthAiCalculate11.PatternMatching(nxsqlHelper);
            label59.Text = "计划完成率:" + result11.AchievePercent.ToString() + "%";
            label61.Text = "非计划率:" + result11.NoAchievePercent.ToString() + "%";
            label60.Text = "生产难度:" + result11.DifficultyValue.ToString();

            ClosedForm(resultForm1);
            ClosedForm(resultForm2);
            ClosedForm(resultForm3);
            ClosedForm(resultForm4);
            ClosedForm(resultForm5);
            ClosedForm(resultForm6);
            ClosedForm(resultForm7);
            ClosedForm(resultForm8);
            ClosedForm(resultForm9);
            ClosedForm(resultForm10);
            ClosedForm(resultForm11);

            resultForm1 = new ResultForm(result1, "方案1", SheetName(), resultTable, jhdt);
            resultForm2 = new ResultForm(result2, "方案2", SheetName(), resultTable, jhdt);
            resultForm3 = new ResultForm(result3, "方案3", SheetName(), resultTable, jhdt);
            resultForm4 = new ResultForm(result4, "方案4", SheetName(), resultTable, jhdt);
            resultForm5 = new ResultForm(result5, "方案5", SheetName(), resultTable, jhdt);
            resultForm6 = new ResultForm(result6, "方案6", SheetName(), resultTable, jhdt);
            resultForm7 = new ResultForm(result7, "方案7", SheetName(), resultTable, jhdt);
            resultForm8 = new ResultForm(result8, "方案8", SheetName(), resultTable, jhdt);
            resultForm9 = new ResultForm(result9, "方案9", SheetName(), resultTable, jhdt);
            resultForm10 = new ResultForm(result10, "方案10", SheetName(), resultTable, jhdt);
            resultForm11 = new ResultForm(result11, "方案11", SheetName(), resultTable, jhdt);

            g1.ForeColor = System.Drawing.Color.Black;
            g2.ForeColor = System.Drawing.Color.Black;
            g3.ForeColor = System.Drawing.Color.Black;
            g4.ForeColor = System.Drawing.Color.Black;
            g5.ForeColor = System.Drawing.Color.Black;
            g6.ForeColor = System.Drawing.Color.Black;
            g7.ForeColor = System.Drawing.Color.Black;
            g8.ForeColor = System.Drawing.Color.Black;
            g9.ForeColor = System.Drawing.Color.Black;
            g10.ForeColor = System.Drawing.Color.Black;
            g11.ForeColor = System.Drawing.Color.Black;

            Dictionary<int, double> dic = new Dictionary<int, double>
            {
                { 1, (result1.AchievePercent - result1.NoAchievePercent) / result1.DifficultyValue },
                { 2, (result2.AchievePercent - result2.NoAchievePercent) / result2.DifficultyValue },
                { 3, (result3.AchievePercent - result3.NoAchievePercent) / result3.DifficultyValue },
                { 4, (result4.AchievePercent - result4.NoAchievePercent) / result4.DifficultyValue },
                { 5, (result5.AchievePercent - result5.NoAchievePercent) / result5.DifficultyValue },
                { 6, (result6.AchievePercent - result6.NoAchievePercent) / result6.DifficultyValue },
                { 7, (result7.AchievePercent - result7.NoAchievePercent) / result7.DifficultyValue },
                { 8, (result8.AchievePercent - result8.NoAchievePercent) / result8.DifficultyValue },
                { 9, (result9.AchievePercent - result9.NoAchievePercent) / result9.DifficultyValue },
                { 10, (result10.AchievePercent - result10.NoAchievePercent) / result10.DifficultyValue },
                { 11, (result11.AchievePercent - result11.NoAchievePercent) / result11.DifficultyValue }
        };
            var d = dic.OrderByDescending(o => o.Value).First();
            int i = d.Key;
            switch (i)
            {
                case 1:
                    GroupChangeColer(g1);
                    break;

                case 2:
                    GroupChangeColer(g2);
                    break;

                case 3:
                    GroupChangeColer(g3);
                    break;

                case 4:
                    GroupChangeColer(g4);
                    break;

                case 5:
                    GroupChangeColer(g5);
                    break;

                case 6:
                    GroupChangeColer(g6);
                    break;

                case 7:
                    GroupChangeColer(g7);
                    break;

                case 8:
                    GroupChangeColer(g8);
                    break;

                case 9:
                    GroupChangeColer(g9);
                    break;

                case 10:
                    GroupChangeColer(g10);
                    break;
            }
        }

        private void GroupChangeColer(GroupBox groupBox)
        {
            groupBox.ForeColor = System.Drawing.Color.Red;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0 || dataGridView2.Rows.Count == 0)
            {
                MessageBox.Show("请先打开计划数据表！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Stopwatch sw = new Stopwatch();
            sw.Start();
            DataTable _scdt = (DataTable)dataGridView1.DataSource;
            DataTable _jhdt = (DataTable)dataGridView2.DataSource;
            DataTable scdtTemp = new DataTable();
            scdtTemp.Columns.Add("lg", typeof(decimal));
            scdtTemp.Columns.Add("num", typeof(int));
            foreach (DataRow item in _scdt.Rows)
            {
                DataRow dataRow = scdtTemp.NewRow();
                dataRow["lg"] = item["lg"];
                dataRow["num"] = item["num"];
                scdtTemp.Rows.Add(dataRow);
            }
            DataTable jhdtTemp = new DataTable();
            jhdtTemp.Columns.Add("lg", typeof(decimal));
            jhdtTemp.Columns.Add("num", typeof(int));
            foreach (DataRow item in _jhdt.Rows)
            {
                DataRow dataRow = jhdtTemp.NewRow();
                dataRow["lg"] = item["lg"];
                dataRow["num"] = item["num"];
                jhdtTemp.Rows.Add(dataRow);
            }
            jhdt = Data.ChangeDataTableDataType(_jhdt);
            scdt = Data.ChangeDataTableDataType(_scdt);
            C();
            SetOpenLabel(false);
            sw.Stop();
            TimeSpan ts = sw.Elapsed;
            MessageBox.Show("计算成功，耗时:" + ts.Hours.ToString() + "小时" + ts.Minutes.ToString() + "分钟" + ts.Seconds + "秒" + ts.Milliseconds + "毫秒", "提示！");
        }

        private void ShowResult(AiResult aiResult, ResultForm resultForm, string title, Dictionary<double, ResultTable> resultTable)
        {
            if (resultForm != null)
            {
                if (resultForm.IsDisposed)
                {
                    resultForm = new ResultForm(aiResult, title, SheetName(), resultTable, jhdt);
                }
                resultForm.Show();
                resultForm.Activate();
            }
            else
            {
                MessageBox.Show("请先计算后再打开结果！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Label29_Click(object sender, EventArgs e)
        {
            ShowResult(result1, resultForm1, "方案1", resultTable);
        }

        private void Label30_Click(object sender, EventArgs e)
        {
            ShowResult(result2, resultForm2, "方案2", resultTable);
        }

        private void Label31_Click(object sender, EventArgs e)
        {
            ShowResult(result3, resultForm3, "方案3", resultTable);
        }

        private void Label32_Click(object sender, EventArgs e)
        {
            ShowResult(result4, resultForm4, "方案4", resultTable);
        }

        private void Label33_Click(object sender, EventArgs e)
        {
            ShowResult(result5, resultForm5, "方案5", resultTable);
        }

        private void Label34_Click(object sender, EventArgs e)
        {
            ShowResult(result6, resultForm6, "方案6", resultTable);
        }

        private void Label35_Click(object sender, EventArgs e)
        {
            ShowResult(result7, resultForm7, "方案7", resultTable);
        }

        private void Label36_Click(object sender, EventArgs e)
        {
            ShowResult(result8, resultForm8, "方案8", resultTable);
        }

        private void Label37_Click(object sender, EventArgs e)
        {
            ShowResult(result9, resultForm9, "方案9", resultTable);
        }

        private void Label45_Click(object sender, EventArgs e)
        {
            ShowResult(result10, resultForm10, "方案10", resultTable);
        }

        private void CalculateMain_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Properties.Settings.Default.DbConn) || string.IsNullOrEmpty(Properties.Settings.Default.SbDbConn))
            {
                SetDataConn setDataConn = new SetDataConn();
                setDataConn.ShowDialog();
            }
            SetDataGridDefault(dataGridView1);
            SetDataGridDefault(dataGridView2);
            dataGridView1.ReadOnly = false;
            dataGridView2.ReadOnly = false;
            SetOpenLabel(true);
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            double.TryParse(textBox11.Text, out double lg);
            if (lg <= 0)
            {
                MessageBox.Show("长度必须大于0！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int.TryParse(textBox12.Text, out int num);
            if (num < 0)
            {
                MessageBox.Show("支数必须大于0！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DataTable dt = (DataTable)dataGridView1.DataSource;
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("lg", typeof(double));
                dt.Columns.Add("num", typeof(int));
            }
            DataRow dataRow = dt.NewRow();
            dataRow["lg"] = lg;
            dataRow["num"] = num;
            dt.Rows.InsertAt(dataRow, 0);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].HeaderText = "长度";
            dataGridView1.Columns[1].HeaderText = "支数";
        }

        private void 移除本行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataGridView dg = (DataGridView)GetOwnerContextMenuStrip(sender);
            if (dg.SelectedRows.Count > 0)
            {
                dg.Rows.RemoveAt(dg.SelectedRows[0].Index);
            }
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            List<string> groupString = new List<string>();
            DataTable listDt = new DataTable();
            listDt.Columns.Add("cutGroup");
            listDt.Columns.Add("cutGuid");
            listDt.Columns.Add("mLength");
            listDt.Columns.Add("mainLength");
            listDt.Columns.Add("mainNum");
            listDt.Columns.Add("mKey");

            DataTable listItemDt = new DataTable();
            listItemDt.Columns.Add("length");
            listItemDt.Columns.Add("num");
            listItemDt.Columns.Add("cutGuid");
            for (double length1 = 6; length1 <= 20; length1 += 0.5)
            {
                for (int i = 1; i < 9; i++)
                {
                    for (double length2 = 6; length2 <= 20; length2 += 0.5)
                        for (int j = 1; j < 9 - i; j++)
                        {
                            double suml = length1 * i + length2 * j;
                            for (double L = 24; L < 60; L += 0.5)
                            {
                                if (suml == L)
                                {
                                    string guid = Guid.NewGuid().ToString();
                                    string cutgroup;
                                    if (length1 == length2)
                                    {
                                        cutgroup = L.ToString() + "=" + length1.ToString() + "*" + (i + j).ToString();
                                        if (!groupString.Contains(cutgroup))
                                        {
                                            DataRow row = listDt.NewRow();
                                            row["cutGroup"] = cutgroup;
                                            row["cutGuid"] = guid;
                                            row["mLength"] = L;
                                            row["mainLength"] = length1;
                                            row["mainNum"] = i + j;
                                            row["mKey"] = cutgroup; ;
                                            listDt.Rows.Add(row);
                                            groupString.Add(cutgroup);
                                        }
                                    }
                                    else
                                    {
                                        cutgroup = L.ToString() + "=" + length1.ToString() + "*" + i.ToString() + "+" + length2.ToString() + "*" + j.ToString();
                                        string cutgroup1 = L.ToString() + "=" + length2.ToString() + "*" + j.ToString() + "+" + length1.ToString() + "*" + i.ToString();
                                        string mkey = "";
                                        if (length1 <= length2)
                                        {
                                            mkey = cutgroup;
                                        }
                                        else
                                        {
                                            mkey = cutgroup1;
                                        }
                                        if (!groupString.Contains(cutgroup))
                                        {
                                            DataRow row = listDt.NewRow();
                                            row["cutGroup"] = cutgroup;
                                            row["cutGuid"] = guid;
                                            row["mLength"] = L;
                                            row["mainLength"] = length1;
                                            row["mainNum"] = i;
                                            row["mKey"] = mkey; ;
                                            listDt.Rows.Add(row);
                                            groupString.Add(cutgroup);
                                        }
                                        DataRow itemRowJ = listItemDt.NewRow();
                                        itemRowJ["length"] = length2;
                                        itemRowJ["num"] = j;
                                        itemRowJ["cutGuid"] = guid;
                                        listItemDt.Rows.Add(itemRowJ);
                                    }
                                }
                            }
                        }
                }
            }
            if (listDt.Rows.Count > 0)
            {
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SysInit.DecryptDbConn()))
                {
                    bulkCopy.DestinationTableName = "LA_CutTable";//在插入的目标表                                                             //DataTable列名与数据库列名的映射
                    bulkCopy.ColumnMappings.Add("cutGroup", "cutGroup");
                    bulkCopy.ColumnMappings.Add("cutGuid", "cutGuid");
                    bulkCopy.ColumnMappings.Add("mLength", "mLength");
                    bulkCopy.ColumnMappings.Add("mainLength", "mainLength");
                    bulkCopy.ColumnMappings.Add("mainNum", "mainNum");
                    bulkCopy.ColumnMappings.Add("mKey", "mKey");

                    bulkCopy.WriteToServer(listDt);//写入到数据库中
                }
            }
            if (listItemDt.Rows.Count > 0)
            {
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SysInit.DecryptDbConn()))
                {
                    bulkCopy.DestinationTableName = "LA_CutLengthItem";//在插入的目标表                                                             //DataTable列名与数据库列名的映射
                    bulkCopy.ColumnMappings.Add("length", "length");
                    bulkCopy.ColumnMappings.Add("num", "num");
                    bulkCopy.ColumnMappings.Add("cutGuid", "cutGuid");
                    bulkCopy.WriteToServer(listItemDt);//写入到数据库中
                }
            }
            ShowMsg("添加成功！");
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            double.TryParse(textBox13.Text, out double lg);
            if (lg <= 0)
            {
                MessageBox.Show("长度必须大于0！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int.TryParse(textBox14.Text, out int num);
            if (num < 0)
            {
                MessageBox.Show("支数必须大于0！", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var ys = lg % 0.5;
            if ((lg % 0.5) > 0)
            {
                MessageBox.Show("只能处理0.5倍定尺", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DataTable dt = (DataTable)dataGridView2.DataSource;
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("lg", typeof(double));
                dt.Columns.Add("num", typeof(int));
            }
            DataRow dataRow = dt.NewRow();
            dataRow["lg"] = lg;
            dataRow["num"] = num;
            dt.Rows.Add(dataRow);
            dataGridView2.DataSource = dt;
            dataGridView2.Columns[0].HeaderText = "长度";
            dataGridView2.Columns[1].HeaderText = "支数";
        }

        private void Label58_Click(object sender, EventArgs e)
        {
            ShowResult(result11, resultForm11, "方案11", resultTable);
        }
    }
}